#pragma once
#include <google/protobuf/descriptor.h>
#include <google/protobuf/message.h>
#include"Logger.h"
#include"Buffer.h"
using namespace Log;
namespace wjc {
    //class ErrorCode;
    using  MessagePtr = std::shared_ptr<google::protobuf::Message>;
    class Parser {
    public:
        enum ErrorCode
        {
            kNoError = 0,
            kInvalidLength,
            kCheckSumError,
            kInvalidNameLen,
            kUnknownMessageType,
            kParseError,
        };
    private:
        const static int kHeaderLen = sizeof(int32_t);
        const static int kMinMessageLen = 2 * kHeaderLen + 2; // nameLen + typeName + checkSum
        const static int kMaxMessageLen = 64 * 1024 * 1024; // same as codec_stream.h kDefaultTotalBytesLimit

        static int32_t asInt32(const char* buf);

        static MessagePtr parse(const char* buf, int len, ErrorCode* error);
    public:

        static MessagePtr parse(Buffer* buf);

        static google::protobuf::Message* createMessage(const std::string& typeName);

        static bool writeToBuffer(Buffer* buf, const google::protobuf::Message& message, int& toal_len);

        static MessagePtr getAliveMsg(const std::string& name);

    };
}
